package com.example.bmanliku;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private EditText tvUserName;
    private EditText tvPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tvUserName = (EditText) findViewById(R.id.userId);
        tvPassword = (EditText) findViewById(R.id.userPwd);
        btnLogin = (Button) findViewById(R.id.loginBtn);

        btnLogin.setOnClickListener(this);
    }

/*
    public void LoginClick(View view) {
        Intent intent=new Intent(MainActivity.this,IndexActivity.class);
        startActivity(intent);
    }

*/
    public void RegisterClick(View view) {
        Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
        startActivity(intent);
    }

    @Override
    public void onClick(@org.jetbrains.annotations.NotNull View v) {
        switch (v.getId()) {
            case R.id.loginBtn:
                submit();
                break;
        }
    }
    private void submit() {
        // validate
        String tvUserNameString = tvUserName.getText().toString().trim();
        if (TextUtils.isEmpty(tvUserNameString)) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        String tvPasswordString = tvPassword.getText().toString().trim();
        if (TextUtils.isEmpty(tvPasswordString)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        //  validate success, do something
            InternetSever(tvUserNameString,tvPasswordString);
    }
    private void InternetSever(String username,String password) {
        OkHttpClient client = new OkHttpClient();
        FormBody body = new FormBody.Builder()
                .add("username", username)
                .add("password", password)
                .build();
        final Request request = new Request.Builder()
                .url("http://192.168.1.7:8000/login/")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback()
        {

            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(MainActivity.this, "网络异常，请稍后再试！", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException
            {

                final String date=response.body().string();
                         Gson gson=new Gson();
                        Main_cs main_cs=gson.fromJson(date,Main_cs.class);
                        if (main_cs.get_$StatusCode185()==200){
                            //Toast.makeText(MainActivity.this, "欢迎登录，", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(MainActivity.this, IndexActivity.class);
                            startActivity(intent);
                        }else {
                            Toast.makeText(MainActivity.this, Main_cs.getCode(), Toast.LENGTH_SHORT).show();
                            tvPassword.setText("");
                            tvUserName.setText("");
                };

            }
        });

    }
}